<template>
  <div>
    <el-container>
      <el-header height="70px">
        <div style="height:20px; font-size: 15px;background-color: rgba(205,205,205,0.2)">
          <div style="float: left; margin-left: 20px">
            <div>系统模块 > 操作管理</div>
          </div>
        </div>
        <div style="height: 40px;margin-top: 10px;min-height: 60px">
          <el-form ref="selectOper" :model="selectOper" :inline="true" label-width="75px">
            <el-col :span="7">
              <el-form-item label="操作名称" prop="operName">
                <el-input size="mini" v-model="selectOper.operName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="操作url" prop="operUrl">
                <el-input size="mini" v-model="selectOper.operUrl"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-button size="mini" type="primary" @click="getOperList()">确定</el-button>
              <el-button size="mini" @click="toRestFild('selectOper')">重置</el-button>
            </el-col>
          </el-form>
        </div>
      </el-header>
      <el-main>
        <div style="float: left">
          <el-button @click="addOper()" type="danger" size="mini">添加</el-button>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="id"
          height="350"
          default-expand-all>
          <el-table-column
            prop="operName"
            label="操作名称">
          </el-table-column>
          <el-table-column
            prop="operUrl"
            label="url路径"
            sortable>
          </el-table-column>
          <el-table-column label="操作"
                           align="center"
          >
            <template slot-scope="scope">
              <el-button @click.native.prevent="editOper(scope)"
                         type="text"
              >编辑
              </el-button>
              <el-button @click.native.prevent="deleteOper(scope)"
                         type="text"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage1"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
        </el-pagination>
        <el-dialog
          :before-close="handleClose"
          :title="title"
          :visible.sync="dialogVisible"
          width="40%">
          <div>
            <el-form ref="oper" :model="oper" label-position="right" label-width="150px">
              <el-form-item label="id:" prop="id">
                <el-input v-model="oper.id" style="width: 80%" disabled></el-input>
              </el-form-item>
              <el-form-item :rules="[{ required: true, message: '请输入操作名称', trigger: 'blur' }]" label="操作名称:"
                            prop="operName">
                <el-input v-model="oper.operName" style="width: 80%"></el-input>
              </el-form-item>
              <el-form-item :rules="[{ required: true, message: '请输入操作url', trigger: 'blur' }]" label="操作url:"
                            prop="operUrl">
                <el-input v-model="oper.operUrl" style="width: 80%"></el-input>
              </el-form-item>
            </el-form>
          </div>
          <span slot="footer" class="dialog-footer">
                    <el-button @click="cancel()">取 消</el-button>
                  <el-button type="primary" @click="sureOper()">确 定</el-button>
                </span>
        </el-dialog>
      </el-main>
    </el-container>

  </div>
</template>

<script>
export default {
  name: "operModule",
  data() {
    return {
      tableData: [],
      selectOper: {
        operName: '',
        operUrl: '',
      },
      title: '',
      dialogVisible: false,
      oper: {
        id: '',
        operName: '',
        operUrl: ''
      },
      currentPage1: 1,
      pageSize: 10,
      total: 0,

    }
  },
  mounted() {
    this.getOperList();
  },
  methods: {
    getOperList() {
      this.$axios({
        url: '/oper/getOperList',
        method: "get",
        params: {
          operName: this.selectOper.operName,
          operUrl: this.selectOper.operUrl,
          pageNo: this.currentPage1,
          pageSize: this.pageSize
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
        } else {
          this.$message.error("系统出错，请联系管理员")
        }
      })
    },
    toRestFild(ref) {
      this.$nextTick(() => {
        this.$refs[ref].resetFields()
      })
    },
    handleClose() {
      this.toRestFild('oper')
      this.dialogVisible = false
    },
    handleCurrentChange(val) {
      this.currentPage1 = val
      this.getOperList()
    },
    editOper(scope) {
      let row = scope.row
      this.title = '修改操作'
      this.oper = {
        id: row.id,
        operName: row.operName,
        operUrl: row.operUrl
      }
      this.dialogVisible = true
    },
    addOper() {
      this.title = '新增操作'
      this.$nextTick(() => {
        this.$refs['oper'].resetFields()
      })
      this.dialogVisible = true
    },
    sureOper() {
      let url = this.title === '新增操作' ? '/oper/addOper' : '/oper/editOper'
      this.$refs['oper'].validate(res => {
          if (res) {
            this.$axios({
              url: url,
              method: 'post',
              data: {
                ...this.oper
              }
            }).then(res => {
              if (res.data.code === 200) {
                this.$message.success("操作成功！")
                this.getOperList()
                this.toRestFild('oper')
                this.dialogVisible = false
              } else {
                this.$message.error("操作失败！")
              }
            })
          }
        }
      )
    },
    deleteOper(scope) {
      this.$confirm('确认删除？')
        .then(_ => {
          let id = scope.row.id
          this.$axios({
            url: '/oper/deleteOper',
            method: 'delete',
            params: {
              id: id
            }
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success("删除成功！")
              this.getOperList()
            } else {
              this.$message.error("删除失败！")
            }
          })
        })
        .catch(_ => {
        });
    },
    cancel() {
      this.toRestFild('oper')
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
